/**
 * ReportGear(2011)
 */
package net.abbp.report.utils;

import java.awt.Color;

/**
 * 颜色实用类
 * 
 * @version 1.0 2011-4-14
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class ColorUtils {
    // Color转换为16进制显示
    public static String toHexEncoding(Color color) {
        String R, G, B, A;
        StringBuffer sb = new StringBuffer();
        R = Integer.toHexString(color.getRed());
        G = Integer.toHexString(color.getGreen());
        B = Integer.toHexString(color.getBlue());
        A = Integer.toHexString(color.getAlpha());

        A = A.length() == 1 ? "0" + A : A;
        R = R.length() == 1 ? "0" + R : R;
        G = G.length() == 1 ? "0" + G : G;
        B = B.length() == 1 ? "0" + B : B;

        sb.append(A);
        sb.append(R);
        sb.append(G);
        sb.append(B);

        return sb.toString();

    }

    // 把字符串表达的颜色值转换成java.awt.Color
    public static Color parseToColor(final String c) {
        Color convertedColor = Color.BLACK;
        try {
            convertedColor = new Color((int) Long.parseLong(c, 16), true);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            // codes to deal with this exception
        }

        return convertedColor;
    }
}
